package com.uenpay.im.ui.old.activity.mycollect;

import com.qiyunxin.android.http.net.HttpRequestType;
import com.uenpay.im.IMApplication;
import com.uenpay.im.invokeitems.HttpRequestHandle;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-1-25 晚上9:28:19
 * @copyRight http://www.qiyunxin.com
 * @function 收藏管理
 */
public class CollectHandle {

	public interface IGetCollectListResultListener {
		void onGetCollectListResult(int status, String msg,
											 ArrayList<MyCollect> arraylist);
	}

	/**
	 * 获取收藏列表
	 *
	 * @param page
	 * @param mIGetCollectListResultListener
	 */
	public void getCollectList(int page,
			final IGetCollectListResultListener mIGetCollectListResultListener) {
		CollectListInvokeItem item = new CollectListInvokeItem(page);
		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
										 String failReason) {
						if (resultObject != null) {

							CollectListInvokeItem.CollectListInvokeItemResult result = ((CollectListInvokeItem) resultObject)
									.getOutput();
							if (result != null) {
								if (mIGetCollectListResultListener != null) {
									mIGetCollectListResultListener
											.onGetCollectListResult(
													result.status, result.msg,
													result.collect_list);
								}
							} else {
								if (mIGetCollectListResultListener != null) {
									mIGetCollectListResultListener
											.onGetCollectListResult(-1, "",
													null);
								}
							}

						} else {

							if (mIGetCollectListResultListener != null) {
								mIGetCollectListResultListener
										.onGetCollectListResult(-1, "", null);
							}

						}
					}
				});

	}

	public interface ICollectListener {
		void onResult(int status, String msg);
	}

	/**
	 * moduleid[103:聊天内容 , 113:活动 900:武林招徒 等等] not null fcustid : 来源用户id not
	 * null title : 标题 content : 内容 标题和内容不能同时为空 url : 链接 imgurl : 图片地址 targetid
	 * : 目标业务序号(如活动的id ， 招徒信息id等) author : 作者
	 *
	 * @param moduleid
	 *            [103:聊天内容 , 113:活动 900:武林招徒 等等]
	 * @param fcustid
	 *            来源用户id
	 * @param title
	 *            标题
	 * @param author
	 *            作者
	 * @param content
	 *            内容
	 * @param url
	 *            链接
	 * @param imgurl
	 *            图片地址
	 * @param targeid
	 *            目标业务序号(如活动的id ， 招徒信息id等)
	 */
	public void addCollect(int moduleid, String fcustid, String title,
			String author, String content, String url, String imgurl,
			int targeid, final ICollectListener mCollectListener) {
		AddCollectInvokeItem item = new AddCollectInvokeItem(moduleid + "",
				fcustid, title, author, content, url, imgurl, targeid + "");
		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							AddCollectInvokeItem.AddCollectInvokeItemResult result = ((AddCollectInvokeItem) resultObject)
									.getOutput();
							if (result != null) {
								IMApplication.showToast(result.msg);
								if (mCollectListener != null) {
									mCollectListener.onResult(result.status,
											result.msg);
								}
							} else {
								if (mCollectListener != null) {
									mCollectListener.onResult(-1, failReason);
								}
							}
						} else {
							if (mCollectListener != null) {
								mCollectListener.onResult(-1, failReason);
							}
						}
					}
				});
	}

	/**
	 * 删除收藏
	 *
	 * @param favorite_id
	 * @param mICollectListener
	 */
	public void deleteCollect(final int favorite_id,
			final ICollectListener mICollectListener) {
		DeleteCollectInvokeItem item = new DeleteCollectInvokeItem(favorite_id);

		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							DeleteCollectInvokeItem.DeleteCollectInvokeItemResult result = ((DeleteCollectInvokeItem) resultObject)
									.getOutput();

							if (result != null) {
								if (mICollectListener != null) {
									mICollectListener.onResult(result.status,
											result.msg);
								}
							} else {
								if (mICollectListener != null) {
									mICollectListener.onResult(-1, failReason);
								}
							}

						} else {
							if (mICollectListener != null) {
								mICollectListener.onResult(-1, failReason);
							}
						}
					}
				});
	}

}
